package com.worldcup.controller.question;

import com.worldcup.common.CommonResult;
import com.worldcup.model.dto.question.QuestionDTO;
import com.worldcup.model.question.Question;
import com.worldcup.model.question.QuestionOption;
import com.worldcup.service.question.QuestionOptionService;
import com.worldcup.service.question.QuestionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author duwan.zq
 * @date 2019/03/05
 */
@RestController
@RequestMapping("/api/java/question")
public class QuestionController {

    private final static Logger logger= LoggerFactory.getLogger(QuestionController.class);

    @Autowired
    private QuestionService questionService;

    @Autowired
    private QuestionOptionService optionService;

    @RequestMapping("/list")
    public CommonResult listQuestion(){
        //todo 可以缓存起来
        List<QuestionDTO> result = new ArrayList<>();
        List<Question> questionList = questionService.listQuestion();
        for(Question question : questionList){
            List<QuestionOption> options = optionService.getOptionList(question.getId());
            result.add(new QuestionDTO(question, options));
        }
        return CommonResult.buildSuccess(result);
    }



}
